package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.mapper.*;
import cn.wolfcode.trip.base.service.ILoveService;
import cn.wolfcode.trip.base.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ILoveServiceImpl implements ILoveService {

    @Autowired
    private LoveMapper loveMapper;
    @Autowired
    private TravelMapper travelMapper;
    @Autowired
    private StrategyCommentMapper commentMapper;
    @Autowired
    private StrategyMapper strategyMapper;
    @Autowired
    private FriendCircleMapper friendCircleMapper;

    @Override
    public Boolean saveOrUpdate(Love love) {
        /*User user = UserContext.getCurrentUser();
        if(user!=null) {
            //设置点赞用户
            love.setUser(user);
        }*/
        if (love.getUser() == null) {
            love.setUser(UserContext.getCurrentUser());
        }

        //先查询数据库有没有该数据(存在就更改状态,没有就新增)
        Love data = loveMapper.selectByIdAndType(love);
        //如果数据库没有该对象,就先新增
        if(data == null){
            Long typeId = love.getTypeId();
            //如果前台传来的是游记
            if (love.getType()== Love.TYPE_TRAVEL){
                //根据id查询游记标题
                Travel travel = travelMapper.selectByPrimaryKey(typeId);
                love.setTitle(travel.getTitle());
                //如果类型是游记,该游记的点赞数+1
                int loveNum = travelMapper.selectLoveForId(typeId) +1;
                travelMapper.updateLove(typeId,loveNum);
            }else if(love.getType() == Love.TYPE_STRATEGY_COMMENT){
                //如果类型是评论,查询评论内容
                StrategyComment comment = commentMapper.selectByPrimaryKey(typeId);
                love.setTitle(comment.getContent());
                //如果类型是评论,该评论的点赞数+1
                int loveNum =commentMapper.selectLoveForId(typeId) +1;
                commentMapper.updateLove(typeId,loveNum);
            }else if(love.getType() == Love.TYPE_FRIENDCIRCLE){
                //如果类型是朋友圈,查询朋友圈的内容
                FriendCircle friendCircle = friendCircleMapper.selectByPrimaryKey(typeId);
                love.setTitle(friendCircle.getContent());
                //如果类型是朋友圈动态,需要增加一条中间关系(点赞用户和该动态的关系)
                friendCircleMapper.insertReration(love.getUser().getId(),typeId);
            }
            //设置点赞时间
            love.setCreatTime(new Date());
            loveMapper.insert(love);
            //让前台显示为点赞状态
            return true;

        }else if(data.getState() == false){
            //更改状态,点赞
            data.setState(true);
            Long typeId = data.getTypeId();
            if(data.getType() == Love.TYPE_TRAVEL){
                //如果类型是游记,该游记的点赞数+1
                int loveNum = travelMapper.selectLoveForId(typeId) +1;
                travelMapper.updateLove(typeId,loveNum);
            }else if(data.getType() == Love.TYPE_STRATEGY_COMMENT){
                //如果类型是评论,该评论的点赞数+1
                int loveNum =commentMapper.selectLoveForId(typeId) +1;
                commentMapper.updateLove(typeId,loveNum);
            }else if(data.getType() == Love.TYPE_STRATEGY_COMMENT){
                //如果类型是攻略,该攻略的点赞数+1
                int loveNum = strategyMapper.selectLoveForId(typeId) +1;
                strategyMapper.updateLove(typeId,loveNum);
            }else {
                //如果类型是朋友圈动态,需要增加一条中间关系(点赞用户和该动态的关系)
                friendCircleMapper.insertReration(data.getUser().getId(),data.getTypeId());
            }
            //设置点赞时间
            data.setCreatTime(new Date());
            loveMapper.updateState(data);
        }else{
            //更改状态,取消点赞
            data.setState(false);
            Long typeId = data.getTypeId();
            if(data.getType() == Love.TYPE_TRAVEL){
                //如果类型是游记,该游记的点赞数-1
                int loveNum = travelMapper.selectLoveForId(typeId) -1;
               // travelMapper.updateLove(typeId,loveNum);
            }else if(data.getType() == Love.TYPE_STRATEGY_COMMENT){
                //如果类型是评论,该评论的点赞数-1
                int loveNum =commentMapper.selectLoveForId(typeId) -1;
                commentMapper.updateLove(typeId,loveNum);
            }else if(data.getType() == Love.TYPE_STRATEGY_COMMENT){
                //如果类型是攻略,该攻略的点赞数-1
                int loveNum =strategyMapper.selectLoveForId(typeId) -1;
                strategyMapper.updateLove(typeId,loveNum);
            }else{
                //如果类型是朋友圈动态,需要删除一条中间关系(点赞用户和该动态的关系)
                friendCircleMapper.deleteReration(data.getUser().getId(),data.getTypeId());
            }
            loveMapper.updateState(data);
        }
        return data.getState();
    }


    @Override
    public int selectForReadCount(Long authorId) {
        //设置未读
        Boolean isRead = false;
        return loveMapper.selectForReadCount(authorId,isRead);
    }

    @Override
    public Boolean selectState(Love love) {
        //设置点赞用户
        love.setUser(UserContext.getCurrentUser());
        Love data = loveMapper.selectByIdAndType(love);
        if(data == null){
            return false;
        }
        return data.getState();
    }

    @Override
    public void updateIsRead(Long authorId) {
        //设置已读
        Boolean isRead = true;
        loveMapper.updateIsRead(authorId, isRead);
    }

    @Override
    public List<Love> selectForLovesByAuthorId(Long authorId) {
        return loveMapper.selectByAuthorId(authorId);
    }
}
